package com.netintech.automation.utils;

import com.netintech.automation.bean.DbBean;
import com.netintech.automation.bean.DbStatic;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 生成java实体类
 */
public class GenerateJava {

    //实体类头部
    public static StringBuffer BEAN_TOP_TEMPLATE = null;
    //实体类属性
    public static StringBuffer BEAN_ATTRIBUTE_TEMPLATE = null;
    //实体类GET方法
    public static StringBuffer BEAN_GET_TEMPLATE = null;
    //实体类SET方法
    public static StringBuffer BEAN_SET_TEMPLATE = null;

    static{
        BEAN_TOP_TEMPLATE = new StringBuffer();
        BEAN_ATTRIBUTE_TEMPLATE = new StringBuffer();
        BEAN_GET_TEMPLATE = new StringBuffer();
        BEAN_SET_TEMPLATE = new StringBuffer();
    }
    /**
     * 生成java代码方法
     * @param model
     * @return
     */
    public static String GenerateJavaCode(DbBean model){
        StringBuffer str = new StringBuffer();
        str.append(get_filetop(model));
        str.append(get_fileattribute(model));
        str.append(get_fileGetMethon(model));
        str.append(get_fileSetMethon(model));
        str.append(get_fileToStringHascodeEqualsMethon(model));
        return str.toString()+"\n}";
    }

    private static String get_fileToStringHascodeEqualsMethon(DbBean model) {
        StringBuffer strBuf = new StringBuffer();
        strBuf.append("\tpublic String toString(){return ToStringBuilder.reflectionToString(this);}\n\n");
        strBuf.append("\tpublic int hasCode(){return new HashCodeBuilder().append(getId()).toHashCode();}\n\n");
        strBuf.append("\tpublic boolean equals(Object obj){\n");
        strBuf.append("\t\tif(obj instanceof ShopAttr == false) return false;\n");
        strBuf.append("\t\tif(this == obj) return true;\n");
        strBuf.append("\t\t"+captureName(model.getTablename())+" other = ("+captureName(model.getTablename())+")obj\n");
        strBuf.append("\t\treturn new EqualsBuilder()\n");
        strBuf.append("\t\t.append(getId(),other.getId())\n");
        strBuf.append("\t\t.isEquals();\n\t}\n");
        return strBuf.toString();
    }

    public static String get_fileSetMethon(DbBean model){
        BEAN_SET_TEMPLATE.setLength(0);
        String [] obj = model.getZdname().split(",");
        for (String value:obj){
            String [] svalue = value.split("--");
            String key = DbStatic.DB_BEAN_ODBC.get(svalue[1].toUpperCase()).toString();
            key = key.substring(key.indexOf(".",key.indexOf(".")+1)+1,key.length());
            String moth = svalue[0].toLowerCase();
            BEAN_SET_TEMPLATE.append("\tpublic void set" +captureName(moth)+"("+key+" "+moth+"){\n");
            BEAN_SET_TEMPLATE.append("\t\tthis."+moth +" = "+moth+";\n");
            BEAN_SET_TEMPLATE.append("\t}\n\n");
        }
        return BEAN_SET_TEMPLATE.toString();
    }

    public static String get_fileGetMethon(DbBean model){
        BEAN_GET_TEMPLATE.setLength(0);
        String [] obj = model.getZdname().split(",");
        for (String value:obj){
            String [] svalue = value.split("--");
            String key = DbStatic.DB_BEAN_ODBC.get(svalue[1].toUpperCase()).toString();
            key = key.substring(key.indexOf(".",key.indexOf(".")+1)+1,key.length());
            String moth = svalue[0].toLowerCase();
            BEAN_GET_TEMPLATE.append("\tpublic " +key+ " get" +captureName(moth) +"(){\n");
            BEAN_GET_TEMPLATE.append("\t\treturn "+moth +";\n");
            BEAN_GET_TEMPLATE.append("\t}\n\n");
        }
        return BEAN_GET_TEMPLATE.toString();
    }


    public static String get_fileattribute(DbBean model){
        StringBuffer strBuf = new StringBuffer();
        strBuf.append("\t//alias\n");
        BEAN_ATTRIBUTE_TEMPLATE.setLength(0);
        BEAN_ATTRIBUTE_TEMPLATE.append("\t//columns START\n");
        String [] obj = model.getZdname().split(",");
        for (String value:obj){
            String [] svalue = value.split("--");
            String key = DbStatic.DB_BEAN_ODBC.get(svalue[1].toUpperCase()).toString();
            key = key.substring(key.indexOf(".",key.indexOf(".")+1)+1,key.length());
            BEAN_ATTRIBUTE_TEMPLATE.append("\t//"+svalue[2]+"\t\t\tdb_column: "+svalue[0].toUpperCase()+"\n");
            BEAN_ATTRIBUTE_TEMPLATE.append("\tprivate " +key+ " "+captureNameTwo(svalue[0])+";\n\n");
            strBuf.append("\tpublic static final String ALIAS_"+svalue[0].toUpperCase()+" = \""+svalue[2]+"\";\n");

        }
        BEAN_ATTRIBUTE_TEMPLATE.append("\t//columns END\n\n");
        strBuf.append("\n\n").append(BEAN_ATTRIBUTE_TEMPLATE);
        return strBuf.toString();
    }

    public static String get_filetop(DbBean model){
        BEAN_TOP_TEMPLATE.setLength(0);
        BEAN_TOP_TEMPLATE.append("package "+model.getCompages()+".bean;\n\n");
        String [] obj = model.getZdname().split(",");
        Map<String,String> map = new HashMap<String,String>();
        for (String value:obj){
            String [] svalue = value.split("--");
            map.put(svalue[1].toUpperCase(),svalue[0]);
        }

        for (String key:map.keySet()){
            BEAN_TOP_TEMPLATE.append("import "+ DbStatic.DB_BEAN_ODBC.get(key)+";\n");
        }
        BEAN_TOP_TEMPLATE.append("/**\n");
        BEAN_TOP_TEMPLATE.append("*自动生成实体类\n");
        BEAN_TOP_TEMPLATE.append("* @author AUTOMATION\n");
        BEAN_TOP_TEMPLATE.append("*/\n");
        BEAN_TOP_TEMPLATE.append("public class "+captureName(model.getTablename())+" {\n\n");
        return BEAN_TOP_TEMPLATE.toString();
    }

    /**
     * 首字母大写
     * @param name
     * @return
     */
    public static String captureName(String name) {
        name = captureNameTwo(name);
        name = name.substring(0, 1).toUpperCase() + name.substring(1);
        return  name;

    }

    /**
     * 先全部转为小写，再首字母大写
     * @param name
     * @return
     */
    public static String captureNameTwo(String name) {
        //先全部转为小写，再首字母大写。变态的Oracle、H2表名全部返回大写字母。
         name = StringUtils.lowerCase(name);
        while(StringUtils.indexOf(name, "_") > 0){
            String charString = StringUtils.substring(name, StringUtils.indexOf(name, "_")+1,StringUtils.indexOf(name, "_") + 2);
            name  = StringUtils.replace(name, "_" + charString, charString.toUpperCase());
        }
        return name;
    }

}
